﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using ClassLibrary.Data;
using System.IO;
using System.Text;

public partial class Admin_UserGuide : AdminPage
{
    TblUserGuideDAO UserGuideDAO = new TblUserGuideDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string idGuide = Request.QueryString["IdGuide"];
            if (idGuide != "" || idGuide != null)
            {
                LoadUserGuideById();
            }
        }
    }

    private void LoadUserGuideById()
    {
        string idGuide = Request.QueryString["IdGuide"];
        try
        {
            TblUserGuide userguide = UserGuideDAO.GetUserGuideById(Convert.ToInt32(idGuide));
            txtTitle.Text = userguide.Title;
            txtDesShort.Value = userguide.ShortDescription;
            txtDesDetails.Value = Server.HtmlDecode( userguide.DescriptionDetails);
            drLang.SelectedValue = userguide.IdLanguage.ToString();
            
            imgGuide.ImageUrl = "~/Admin/img/UserGuide/" + userguide.ImageURL;
            if (userguide.Display == true)
                rdDisplayY.Checked = true;
            else
                rdDisplayN.Checked = true;

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
    }
    protected void save_Click(object sender, EventArgs e)
    {
        bool flage = false;
        string idGuide = Request.QueryString["IdGuide"];
        TblUserGuide userguide = new TblUserGuide();
        try 
        {
            userguide.IdLanguage = Convert.ToInt32(drLang.SelectedValue);
            userguide.Title = txtTitle.Text;
            if (rdDisplayY.Checked == true)
            {
                userguide.Display = true;
            }
            if (rdDisplayN.Checked == true)
            {
                userguide.Display = false;
            }

            userguide.ShortDescription = txtDesShort.Value;
            userguide.DescriptionDetails = txtDesDetails.Value;
            string filename = f_name.FileName;

            string path = Server.MapPath("~\\Admin\\img\\UserGuide\\");
            if (idGuide == null || idGuide == "")
            {
                f_name.SaveAs(path + "\\" + filename);
                userguide.ImageURL = filename;
                UserGuideDAO.InsUpdUserGuide(userguide, 1);

            }
            else
            {
                TblUserGuide user = UserGuideDAO.GetUserGuideById(Convert.ToInt32(idGuide));
                filename = user.ImageURL;
                userguide.IDUserGuide = Convert.ToInt32(idGuide);
                
                if (filename != null || filename != "")
                {
                    File.Delete(Server.MapPath("~\\Admin\\img\\UserGuide\\" + f_name));
                }
                f_name.SaveAs(path + "\\" + filename);
                userguide.ImageURL = filename;
                UserGuideDAO.InsUpdUserGuide(userguide, 2);
            }
            flage = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flage)
            Response.Redirect("~/Admin/ListUserGuide.aspx");
    }
}
